package com.qujia.station.service.impl;

import com.qujia.common.utils.MathUtil;
import com.qujia.station.domain.OrderPayType;
import com.qujia.station.domain.PayOrder;
import com.qujia.station.enums.PayTypeNameEnum;
import com.qujia.station.mapper.OrderPayTypeMapper;
import com.qujia.station.mapper.PayOrderHistoryMapper;
import com.qujia.station.service.IPayOrderHistoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 订单支付Service业务层处理
 *
 * @author qujia
 * @date 2020-12-10
 */
@Service
public class PayOrderHistoryServiceImpl implements IPayOrderHistoryService
{
    @Autowired
    private PayOrderHistoryMapper payOrderHistoryMapper;

    @Resource
    private OrderPayTypeMapper orderPayTypeMapper;

    /**
     * 查询订单支付
     *
     * @param orderId 订单支付ID
     * @return 订单支付
     */
    @Override
    public PayOrder selectPayOrderById(Long orderId)
    {
        return payOrderHistoryMapper.selectPayOrderById(orderId);
    }

    /**
     * 查询订单支付
     *
     * @param orderId 订单支付ID
     * @return 订单支付
     */
    @Override
    public PayOrder selectPayOrderForFeiEById(Long orderId){
        return payOrderHistoryMapper.selectPayOrderForFeiEById(orderId);
    }

    /**
     * 查询订单支付列表
     *
     * @param payOrder 订单支付
     * @return 订单支付
     */
    @Override
    public List<PayOrder> selectPayOrderList(PayOrder payOrder)
    {
        // 查询支付方式
        OrderPayType orderPayType = new OrderPayType();
        orderPayType.setStationId(payOrder.getStationId());
        orderPayType.setStationIdList(payOrder.getStationIdList());
        List<PayOrder> payOrders = payOrderHistoryMapper.selectPayOrderList(payOrder);
        List<OrderPayType> orderPayTypeList = orderPayTypeMapper.selectOrderPayType(orderPayType);
        for(PayOrder order : payOrders){
            setPayTypeName(order,orderPayTypeList);
        }
        return payOrders;
    }

    /**
     * 查询总数
     *
     *
     * @return
     */
    @Override
    public int getStatusCount(PayOrder payOrder) {
        int count = payOrderHistoryMapper.countByStatus(payOrder);
        return count;
    }

    /**
     * 查询订单支付列表
     *
     * @param payOrder 订单支付
     * @return 订单支付
     */
    @Override
    public List<PayOrder> selectAllPayOrderList(PayOrder payOrder)
    {
        return payOrderHistoryMapper.selectAllPayOrderList(payOrder);
    }

    @Override
    public List<PayOrder> queryPayOrderHistoryList(PayOrder payOrder){
        return payOrderHistoryMapper.queryPayOrderHistoryList(payOrder);
    }

    @Override
    public void updatePayOrderAmt(Map<String, Object> params) {

        payOrderHistoryMapper.updatePayOrderAmt(params);
    }

    /**
     * 新增订单支付
     *
     * @param payOrder 订单支付
     * @return 结果
     */
    @Override
    public int insertPayOrder(PayOrder payOrder)
    {
        return payOrderHistoryMapper.insertPayOrder(payOrder);
    }

    /**
     * 修改订单支付
     *
     * @param payOrder 订单支付
     * @return 结果
     */
    @Override
    public int updatePayOrder(PayOrder payOrder)
    {
        return payOrderHistoryMapper.updatePayOrder(payOrder);
    }

    /**
     * 批量删除订单支付
     *
     * @param orderIds 需要删除的订单支付ID
     * @return 结果
     */
    @Override
    public int deletePayOrderByIds(Long[] orderIds)
    {
        return payOrderHistoryMapper.deletePayOrderByIds(orderIds);
    }

    @Override
    @Transactional
    public int deletePay(List<Long> list) {
        return payOrderHistoryMapper.deletePay(list);
    }

    /**
     * 删除订单支付信息
     *
     * @param orderId 订单支付ID
     * @return 结果
     */
    @Override
    public int deletePayOrderById(Long orderId)
    {
        return payOrderHistoryMapper.deletePayOrderById(orderId);
    }

    /**
     * 汇总今天的数据
     * @param payOrder
     * @return
     */
    @Override
    public PayOrder selectDaySources(PayOrder payOrder) {
        return payOrderHistoryMapper.selectDaySources(payOrder);
    }

    /**
     * 汇总今天所有的信息
     * @param payOrder
     * @return
     */
    @Override
    public PayOrder selectAllDaySources(PayOrder payOrder) {
        return payOrderHistoryMapper.selectAllDaySources(payOrder);
    }

    /***
     * 获取数据
     * @param payOrder
     * @return
     */
    @Override
    public  List<PayOrder>  selectOilTypeSources(PayOrder payOrder) {
        return payOrderHistoryMapper.selectOilTypeSources(payOrder);
    }

    /***
     * 获取油品的全部数据
     * @param payOrder
     * @return
     */
    @Override
    public  List<PayOrder>  selectAllOilTypeSources(PayOrder payOrder) {
        return payOrderHistoryMapper.selectAllOilTypeSources(payOrder);
    }
    /**
     * 数据概览，获取数据
     * @param payOrder
     * @return
     */

    @Override
    public PayOrder selectOverViewData(PayOrder payOrder) {
        return payOrderHistoryMapper.selectOverViewData(payOrder);
    }

    /**
     * 数据概览，获取数据(包括已经退款的)
     * @param payOrder
     * @return
     */

    @Override
    public PayOrder selectAllOverViewData(PayOrder payOrder) {
        return payOrderHistoryMapper.selectAllOverViewData(payOrder);
    }

    /**
     * 月报数据
     * @param payOrder
     * @return
     */
    @Override
    public List<PayOrder> selectDayReport(PayOrder payOrder) {
        return payOrderHistoryMapper.selectDayReport(payOrder);
    }

    /**
     * 月报数据（包括已经退款的）
     * @param payOrder
     * @return
     */
    @Override
    public List<PayOrder> selectAllDayReport(PayOrder payOrder) {
        return payOrderHistoryMapper.selectAllDayReport(payOrder);
    }
    /**
     * 日报折线图获取油品数据
     * @param payOrder
     * @return
     */
    @Override
    public List<PayOrder> listQydata(PayOrder payOrder) {
        return payOrderHistoryMapper.listQydata(payOrder);
    }
    /**
     * 获取x轴数据
     * @param payOrder
     * @return
     */
    @Override
    public List<PayOrder> listXdata(PayOrder payOrder) {
        return payOrderHistoryMapper.listXdata(payOrder);
    }

    /**
     * 查询导出信息
     * @param payOrder
     * @return
     */
    @Override
    public List<PayOrder> selectPayOrderExport(PayOrder payOrder) {
        return payOrderHistoryMapper.selectPayOrderExport(payOrder);
    }






    /**
     * 日报数据汇总
     * @param payOrder
     * @return
     */
    @Override
    public List<PayOrder> dayOilTypeSources(PayOrder payOrder) {
        return payOrderHistoryMapper.dayOilTypeSources(payOrder);
    }
    /**
     * 日报数据汇总（包括已经退款的）
     * @param payOrder
     * @return
     */
    @Override
    public List<PayOrder> dayAllOilTypeSources(PayOrder payOrder) {
        return payOrderHistoryMapper.dayAllOilTypeSources(payOrder);
    }



    /**
     * 关联用户查询订单支付列表
     * @param payOrder
     * @return
     */
    @Override
    public List<PayOrder> selectUserPayOrderList(PayOrder payOrder) {
        return payOrderHistoryMapper.selectUserPayOrderList(payOrder);
    }


    /**
     * 汇总订单统计信息
     * @param payOrder
     * @return
     */
    @Override
    public PayOrder selectSumPayOrder(PayOrder payOrder){
        return   payOrderHistoryMapper.selectSumPayOrder(payOrder);
    }

    @Override
    public List<PayOrder> selectPayOrderAndWithdrawalAmountList(PayOrder payOrder) {
        List<PayOrder> payOrders = payOrderHistoryMapper.selectPayOrderList(payOrder);
        // 查询支付方式
        OrderPayType orderPayType = new OrderPayType();
        orderPayType.setStationId(payOrder.getStationId());
        orderPayType.setStationIdList(payOrder.getStationIdList());
        List<OrderPayType> orderPayTypeList = orderPayTypeMapper.selectOrderPayType(orderPayType);
        for (PayOrder order : payOrders) {
            if (!StringUtils.equalsAny(order.getStatus(),"0","4") && StringUtils.equalsAny(order.getPayType(),"wx","zfb","pos")) {
                order.setWithdrawalAmount(MathUtil.subtract(order.getAmt(),order.getCommission()));
            }else{
                order.setWithdrawalAmount(BigDecimal.ZERO);
            }
            setPayTypeName(order,orderPayTypeList);
        }
        return payOrders;
    }
    // 匹配支付方式
    private void setPayTypeName(PayOrder payOrder, List<OrderPayType> orderPayTypeList){
        String payTypeName = PayTypeNameEnum.getPayTypeName(payOrder.getPayType());
        if(StringUtils.isNotBlank(payTypeName)){
            payOrder.setPayTypeName(payTypeName);
        }

        for (OrderPayType orderPayType : orderPayTypeList) {
            if(orderPayType.getPayType().equals(payOrder.getPayType())){
                payOrder.setPayTypeName(orderPayType.getPayTypeName());
            }
        }
    }

}
